import { getStream } from '../Core/connect'
import { Vector3 } from '../Math/Vector3'
import { Vector2 } from '../Math/Vector2'
import * as MathUtils from '../Math/MathUtils'

export class DeviceCamera {
    name: string
    uuid: string
    vieoPosition: Vector2
    CameraPostion: Vector3
    CameraRotate: Vector3
    VisualAngle: string
    CameraType: string
    VideoSize: Vector2

    constructor(option: DeviceCameraOption) {
        this.uuid = MathUtils.generateUUID()
        this.name = option.name
        this.VisualAngle = option.VisualAngle
        this.CameraType = option.CameraType
        this.VideoSize = option.VideoSize ? option.VideoSize : new Vector2(100, 100)
        this.vieoPosition = option.vieoPosition ? option.vieoPosition : new Vector2(0, 0)
        this.CameraPostion = option.CameraPostion ? option.CameraPostion : new Vector3(0, 0, 0)
        this.CameraRotate = option.CameraRotate ? option.CameraRotate : new Vector3(0, 0, 0)

        this.init()
    }
    init() {
        const stream = getStream()
        const str = {
            DeviceCamerafunction: 'creatDeviceCamera', 
            ids: `${this.name},${this.uuid}`,
            vieoPosition: `${this.vieoPosition.x},${this.vieoPosition.y},${0}`,
            VideoSize: `${this.VideoSize.x},${this.VideoSize.y},${0}`,
            CameraPostion: `${this.CameraPostion.x},${this.CameraPostion.y},${this.CameraPostion.z}`,
            CameraRotate: `${this.CameraRotate.x},${this.CameraRotate.y},${this.CameraRotate.z}`,
        }
        stream.emitUIInteraction(str)
    }

    // 获取副相机相关数据
    getCameraMatrix() {
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { DeviceCamerafunction: 'getDeviceCameraMatrix', sessionId: sessionId, ids: `${uuid}` }
            stream.emitUIInteraction(message)
            const getMatrixReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) {
                    resolve(returnData)
                    stream.removeResponseEventListener("getDeviceCameraMatrix")
                }
            }
            stream.addResponseEventListener("getDeviceCameraMatrix", getMatrixReady)
        })
    }
    // 设置相机位置
    setCameraPosition( CameraPostion:Vector3){
        this.CameraPostion = CameraPostion
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setCameraPosition', ids: `${this.uuid}`, CameraPostion: `${this.CameraPostion.x},${this.CameraPostion.y},${this.CameraPostion.z}` }
        stream.emitUIInteraction(str)
    }

    // 设置相机旋转
    setCameraRotate(CameraRotate:Vector3){
        this.CameraRotate = CameraRotate
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setCameraRotate', ids: `${this.uuid}`, CameraRotate: `${this.CameraRotate.x},${this.CameraRotate.y},${this.CameraRotate.z}` }
        stream.emitUIInteraction(str)
    }


    //设置副相机画面位置
    setVideoPosition(vieoPosition: Vector2) {
        this.vieoPosition = vieoPosition
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setVideoPosition', ids: `${this.uuid}`, vieoPosition: `${this.vieoPosition.x},${this.vieoPosition.y},${0}` }
        stream.emitUIInteraction(str)
    }
    //设置副相机画面大小
    setVideoSize(VideoSize: Vector2) {
        this.VideoSize = VideoSize
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setVideoSize', ids: `${this.uuid}`, VideoSize: `${this.VideoSize.x},${this.VideoSize.y},${0}` }
        stream.emitUIInteraction(str)
    }
    //设置副相机可视角度，画面方法
    setCameraType(CameraType: string, VisualAngle: string) {
        this.VisualAngle = VisualAngle
        this.CameraType = CameraType
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setCamerType', ids: `${this.uuid}`, CameraType: `${this.CameraType}`, VisualAngle: `${this.VisualAngle}` }
        stream.emitUIInteraction(str)
    }

    //设置副相机朝向
    setCameraLookAt(lookPos: Vector3) {
        let that = this
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { DeviceCamerafunction: 'setCameraLookAt', sessionId: sessionId, ids: `${uuid}`, lookPos: `${lookPos.x},${lookPos.y},${lookPos.z}` }
            stream.emitUIInteraction(message)
            const setCameraLookAtReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) {
                    console.log('获取相机转换后世界变换', returnData)

                    that.CameraRotate.x = returnData.CameraPosition.x
                    that.CameraRotate.y = returnData.CameraPosition.y
                    that.CameraRotate.z = returnData.CameraPosition.z
                    that.CameraPostion.x = returnData.CameraRotate.roll
                    that.CameraPostion.y = returnData.CameraRotate.pitch
                    that.CameraPostion.z = returnData.CameraRotate.yaw


                    resolve(returnData)
                    stream.removeResponseEventListener("setCameraLookAtReady")
                }
            }
            stream.addResponseEventListener("setCameraLookAtReady", setCameraLookAtReady)
        })
    }
    //设置相机朝向对象，并设置是否锁定
    setCameraLookActor(ActorName: string, lockflag: boolean) {
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setCameraLookActor', ids: `${this.uuid}`, ActorName: `${ActorName}`, lockflag: `${lockflag}` }
        stream.emitUIInteraction(str)
    }
    //停止相机锁定actor
    unlockCameraLookActor() {
        const stream = getStream()
        const str = { DeviceCamerafunction: 'UnlockCameraLookActor', ids: `${this.uuid}` }
        stream.emitUIInteraction(str)
    }
    //设置相机父级，跟随父级移动
    setCameraFolowActor(ActorName: string,relativePosition:Vector3,relativeRotate:Vector3 = new Vector3(0,0,0)) {
        const stream = getStream()
        const str = { DeviceCamerafunction: 'setCameraFolowActor', ids: `${this.uuid}`, ActorName: `${ActorName}`,relativePosition: `${relativePosition.x},${relativePosition.y},${relativePosition.z}`, relativeRotate:`${relativeRotate.x},${relativeRotate.y},${relativeRotate.z}`}
        stream.emitUIInteraction(str)
        this.CameraPostion = new Vector3(0,0,0)
    }
    // 解除相机父级跟随吗
    removeCamerFolow() {
        const stream = getStream()
        const str = { DeviceCamerafunction: 'removeCamerFolow', ids: `${this.uuid}` }
        stream.emitUIInteraction(str)
    }
    // 销毁副相机
    destroy() {
        const stream = getStream()
        const str = { DeviceCamerafunction: 'destroy', ids: `${this.uuid}` }
        stream.emitUIInteraction(str)
    }
}
